import { BaseViewProp } from "./BaseViewProp";
import UI_cmp_color from "../../../ui/Main/UI_cmp_color";
import { Data_Vec3 } from "../../debug_inject/common/Data_Vec3";
import ColorEx from "../../../LTGame/Utils/ColorEx";
import { DataHelper } from "../utils/DataHelper";

export class PropColor extends BaseViewProp<UI_cmp_color> {

    public onColorChanged: Laya.Handler;

    private _cacheColor: Data_Vec3;
    private _cacheColorStr: string;

    _OnInited() {
        this.ui.m_color_str.m_input_text.on(Laya.Event.ENTER, this, this._OnTextChanged);
    }

    _OnDispose() {
        if (this.onColorChanged != null) {
            this.onColorChanged.recover();
            this.onColorChanged = null;
        }
    }

    private _OnTextChanged() {
        let wrapColor = ColorEx.HexToV3(this.ui.m_color_str.m_input_text.text);
        let reverseStr = ColorEx.Vec3ToHex(wrapColor);
        this.ui.m_color_str.m_input_text.text = reverseStr;
        if (this._cacheColorStr == reverseStr) return;
        this.onColorChanged?.runWith(new Data_Vec3(wrapColor));
        this._cacheColorStr = reverseStr;
        this._UpdateUI();
    }

    public SetColorVec3(vec3: Data_Vec3) {
        if (this._cacheColor == null) {
            this._cacheColor = vec3;
            this._cacheColorStr = ColorEx.Vec3ToHex(this._cacheColor);
            this._UpdateUI();
        } else if (!DataHelper.IsVec3Same(this._cacheColor, vec3)) {
            this._cacheColor = vec3;
            this._cacheColorStr = ColorEx.Vec3ToHex(this._cacheColor);
            this._UpdateUI();
        }
    }

    private _UpdateUI() {
        this.ui.m_color_str.m_input_text.text = this._cacheColorStr;
        this.ui.m_img_color.color = this._cacheColorStr;
    }

}